package com.zdb.order.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zdb.order.entity.OrderDetail;
import com.zdb.order.mapper.OrderDetailMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author Lx
 * @Date 2020/9/12 19:03
 * @Version 1.0
 */
@Service
public class OrderDetailService {
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 通过一个订单的id和一组已下单的商品id，去订单详情表中查询商品数据
     * @param orderId
     * @param ids
     * @return
     */
    public List<OrderDetail> getOrderDetailByIds(String orderId, String[] ids){
        List<OrderDetail> listOrderDetail = new LinkedList<>();


        for(String id:ids){
            OrderDetail orderDetail = orderDetailMapper.selectOne(new QueryWrapper<OrderDetail>().eq("order_id", orderId).eq("product_id", id));
            if(!StringUtils.isEmpty(orderDetail)){
                orderDetail.setProTotalPrice((double) (orderDetail.getProductPrice() * orderDetail.getProductQuantity()));
                listOrderDetail.add(orderDetail);
            }
        }
        return listOrderDetail;
    }
}
